import { defineStore } from "pinia";
import { AuthState } from "@/stores/interface";
import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList } from "@/utils";
import { getNewRegisterAPI, getStationAdminListAPI } from "@/api/modules/city/organizationAPI";
import { getDispatchListAPI, getDispatchOrderListAPI } from "@/api/modules/city/dispatchAPI";

export const useAuthStore = defineStore({
  id: "geeker-auth",
  state: (): AuthState => ({
    // 按钮权限列表
    authButtonList: {},
    // 菜单权限列表
    authMenuList: [],
    // 当前页面的 router name，用来做按钮权限筛选
    routeName: "",
    tag: {
      new: 0,
      examine: 0,
      passed: 0,
      notPassed: 0,
      cancel: 0
    },
    stationTag: {
      new: 0,
      examine: 0,
      passed: 0,
      notPassed: 0,
      cancel: 0
    },
    dispatchTag: {
      sort: 0,
      pick: 0,
      select: 0,
      take: 0,
      unusual: 0,
      success: 0
    }
  }),
  getters: {
    // 按钮权限列表
    authButtonListGet: state => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: state => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: state => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.authMenuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
  },
  actions: {
    // Get AuthButtonList
    async getAuthButtonList() {
      const { data } = await getAuthButtonListApi();
      this.authButtonList = data as any;
    },
    // Get AuthMenuList
    async getAuthMenuList() {
      const { data } = await getAuthMenuListApi();
      console.log("菜单选择authMenuList", data);
      this.authMenuList = data;
    },
    async setTagsViewNew() {
      const { total } = await getNewRegisterAPI({
        fullName: "",
        auditStatus: "0",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.tag.new = Number(total);
      }
    },
    async setTagsViewExamine() {
      const { total } = await getNewRegisterAPI({
        fullName: "",
        auditStatus: "1",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.tag.examine = Number(total);
      }
    },
    async setTagsViewPassed() {
      const { total } = await getNewRegisterAPI({
        fullName: "",
        auditStatus: "2",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.tag.passed = Number(total);
      }
    },
    async setTagsViewNotPassed() {
      const { total } = await getNewRegisterAPI({
        fullName: "",
        auditStatus: "3",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.tag.notPassed = Number(total);
      }
    },
    async setTagsViewCancel() {
      const { total } = await getNewRegisterAPI({
        fullName: "",
        delFlag: "2",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.tag.cancel = Number(total);
      }
    },
    async setStationTagsViewNew() {
      const { total } = await getStationAdminListAPI({
        fullName: "",
        auditStatus: "0",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.stationTag.new = Number(total);
      } else {
        this.stationTag.new = 0;
      }
    },
    async setStationTagsViewExamine() {
      const { total } = await getStationAdminListAPI({
        fullName: "",
        auditStatus: "1",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.stationTag.examine = Number(total);
      } else {
        this.stationTag.examine = 0;
      }
    },
    async setStationTagsViewPassed() {
      const { total } = await getStationAdminListAPI({
        fullName: "",
        auditStatus: "2",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.stationTag.passed = Number(total);
      } else {
        this.stationTag.passed = 0;
      }
    },
    async setStationTagsViewNotPassed() {
      const { total } = await getStationAdminListAPI({
        fullName: "",
        auditStatus: "3",
        delFlag: "0",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.stationTag.notPassed = Number(total);
      } else {
        this.stationTag.notPassed = 0;
      }
    },
    async setStationTagsViewCancel() {
      const { total } = await getStationAdminListAPI({
        fullName: "",
        // auditStatus: "2",
        delFlag: "2",
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        pageNum: 1,
        pageSize: 10
      });
      if (total) {
        this.stationTag.cancel = Number(total);
      } else {
        this.stationTag.cancel = 0;
      }
    },
    async setDispatchTagsViewSort() {
      const { total } = await getDispatchListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        pageNum: "1",
        pageSize: "10",
        startTime: "",
        endTime: ""
      });
      if (total) {
        this.dispatchTag.sort = Number(total);
      } else {
        this.dispatchTag.sort = 0;
      }
    },
    async setDispatchTagsViewSelect() {
      const { total } = await getDispatchOrderListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        status: "0",
        pageNum: "1",
        pageSize: "10"
      });
      if (total) {
        this.dispatchTag.select = Number(total);
      } else {
        this.dispatchTag.select = 0;
      }
    },
    async setDispatchTagsViewPick() {
      const { total } = await getDispatchOrderListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        status: "2",
        pageNum: "1",
        pageSize: "10"
      });
      if (total) {
        this.dispatchTag.pick = Number(total);
      } else {
        this.dispatchTag.pick = 0;
      }
    },
    async setDispatchTagsViewTake() {
      const { total } = await getDispatchOrderListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        status: "1",
        pageNum: "1",
        pageSize: "10"
      });
      if (total) {
        this.dispatchTag.take = Number(total);
      } else {
        this.dispatchTag.take = 0;
      }
    },
    async setDispatchTagsViewUnusual() {
      const { total } = await getDispatchOrderListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        status: "3",
        pageNum: "1",
        pageSize: "10"
      });
      if (total) {
        this.dispatchTag.unusual = Number(total);
      } else {
        this.dispatchTag.unusual = 0;
      }
    },
    async setDispatchTagsViewSuccess() {
      const { total } = await getDispatchOrderListAPI({
        city: JSON.parse(localStorage.getItem("userInfo") as string).city,
        province: JSON.parse(localStorage.getItem("userInfo") as string).province,
        status: "4",
        pageNum: "1",
        pageSize: "10"
      });
      if (total) {
        this.dispatchTag.success = Number(total);
      } else {
        this.dispatchTag.success = 0;
      }
    },
    // Set RouteName
    async setRouteName(name: string) {
      this.routeName = name;
    }
  }
});
